package gradle.common.core.utils;

import gradle.common.core.exception.InvalidBearerTokenException;
import org.apache.commons.lang3.StringUtils;

public class JwtUtil {

    /**
     * 解析token
     *
     * @param bearerToken bearerToken
     * @return Token
     */
    public  static String extractToken(String bearerToken) {
        if (StringUtils.isBlank(bearerToken)) {
            throw new InvalidBearerTokenException();
        }
        // 去掉 "Bearer " 前缀
        boolean startsWith = bearerToken.startsWith("Bearer ");
        if (!startsWith) {
            throw new InvalidBearerTokenException();
        }
        try {
            return bearerToken.substring(7);
        } catch (Exception e) {
            throw new InvalidBearerTokenException();
        }
    }
}
